﻿
    using Chain.BLL;
    using Chain.Model;
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public partial class FunctionManage : Page
    {

        private Chain.BLL.SysGroupAuthority SysGroupAuthorityBll = new Chain.BLL.SysGroupAuthority();


        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.Page.IsValid)
            {
                Chain.Model.SysModuleAction sysModuleActionModel = new Chain.Model.SysModuleAction();
                int moduleID = ((base.Request["ModuleID"] == null) || (base.Request["ModuleID"] == "-100")) ? 0 : int.Parse(base.Request["ModuleID"]);
                if (moduleID == 0)
                {
                    base.ClientScript.RegisterClientScriptBlock(base.GetType(), Guid.NewGuid().ToString(), "<script>alert('请不要随意篡改地址栏参数!')</script>");
                }
                else
                {
                    try
                    {
                        sysModuleActionModel.ActionCaption = this.txtActionCaption.Text.Trim();
                        sysModuleActionModel.ActionControl = this.txtActionControl.Text.Trim();
                        sysModuleActionModel.ActionRemark = this.txtActionRemark.Text.Trim();
                        sysModuleActionModel.ActionModuleID = new int?(moduleID);
                        int flag = new Chain.BLL.SysModuleAction().Add(sysModuleActionModel);
                        this.txtActionCaption.Text = this.txtActionControl.Text = this.txtActionRemark.Text = "";
                        base.ClientScript.RegisterClientScriptBlock(base.GetType(), Guid.NewGuid().ToString(), "<script>alert('模块动作添加成功')</script>");
                        Chain.BLL.SysGroupAuthority sysGroupAuthorityBll = new Chain.BLL.SysGroupAuthority();
                        Chain.Model.SysGroupAuthority sysGroupAuthorityModel = new Chain.Model.SysGroupAuthority();
                        sysGroupAuthorityModel.GroupID = 1;
                        sysGroupAuthorityModel.ModuleID = sysModuleActionModel.ActionModuleID;
                        sysGroupAuthorityModel.ActionID = new int?(flag);
                        sysGroupAuthorityModel.ActionValue = true;
                        flag = sysGroupAuthorityBll.Add(sysGroupAuthorityModel);
                    }
                    catch (Exception ex)
                    {
                        base.ClientScript.RegisterClientScriptBlock(base.GetType(), Guid.NewGuid().ToString(), "<script>alert('模块动作添加失败：" + ex.Message + "')</script>");
                    }
                    this.gvmFunctionManage.DataBind();
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string moduleID = base.Request["ModuleID"];
            switch (moduleID)
            {
                case null:
                case "-100":
                    base.Response.Redirect("ColumnManage.aspx");
                    return;
            }
            base.Response.Redirect("ModuleManage.aspx?ModuleParentID=" + new Chain.BLL.SysModule().GetModel(int.Parse(moduleID)).ModuleParentID);
        }

        protected void ckbIsPowerReg_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox ckbIsPowerReg = (CheckBox) sender;
            bool isPowerReg = ckbIsPowerReg.Checked;
            int moduleID = Convert.ToInt32(ckbIsPowerReg.Attributes["ModuleID"]);
            int actionID = Convert.ToInt32(ckbIsPowerReg.Attributes["ActionID"]);
            Chain.Model.SysGroupAuthority sysGroupAuthorityModel = new Chain.Model.SysGroupAuthority();
            sysGroupAuthorityModel.GroupID = 1;
            sysGroupAuthorityModel.ModuleID = new int?(moduleID);
            sysGroupAuthorityModel.ActionID = new int?(actionID);
            sysGroupAuthorityModel.ActionValue = isPowerReg;
            this.SysGroupAuthorityBll.UpdateOrEndowPower(sysGroupAuthorityModel);
        }

        protected void gvmFunctionManage_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Chain.Model.SysModuleAction sysModuleActionModel = (Chain.Model.SysModuleAction) e.Row.DataItem;
                bool isPowerReg = this.SysGroupAuthorityBll.isPowerReg(1, sysModuleActionModel.ActionModuleID.Value, sysModuleActionModel.ActionID);
                CheckBox ckbIsPowerReg = (CheckBox) e.Row.FindControl("ckbIsPowerReg");
                ckbIsPowerReg.Checked = isPowerReg;
                ckbIsPowerReg.Attributes.Add("ModuleID", sysModuleActionModel.ActionModuleID.Value.ToString());
                ckbIsPowerReg.Attributes.Add("ActionID", sysModuleActionModel.ActionID.ToString());
            }
        }

        protected void odsFunctionManage_Updated(object sender, ObjectDataSourceStatusEventArgs e)
        {
            if (e.ExceptionHandled)
            {
                base.ClientScript.RegisterClientScriptBlock(base.GetType(), Guid.NewGuid().ToString(), "<script>alert('模块功能修改失败：" + e.Exception.Message + "')</script>");
                e.ExceptionHandled = false;
            }
        }

        protected void odsFunctionManage_Updating(object sender, ObjectDataSourceMethodEventArgs e)
        {
            Chain.Model.SysModuleAction sysModuleActionModel = (Chain.Model.SysModuleAction) e.InputParameters[0];
            bool isOK = true;
            if ((sysModuleActionModel.ActionCaption == null) || (sysModuleActionModel.ActionCaption.Trim() == ""))
            {
                base.ClientScript.RegisterClientScriptBlock(base.GetType(), Guid.NewGuid().ToString(), "<script>alert('动作名称不能为空!')</script>");
                isOK = false;
            }
            if ((sysModuleActionModel.ActionControl == null) || (sysModuleActionModel.ActionControl.Trim() == ""))
            {
                base.ClientScript.RegisterClientScriptBlock(base.GetType(), Guid.NewGuid().ToString(), "<script>alert('控件名称不能为空!')</script>");
                isOK = false;
            }
            if ((sysModuleActionModel.ActionRemark == null) || (sysModuleActionModel.ActionRemark.Trim() == ""))
            {
                base.ClientScript.RegisterClientScriptBlock(base.GetType(), Guid.NewGuid().ToString(), "<script>alert('动作说明不能为空!')</script>");
                isOK = false;
            }
            if (!isOK)
            {
                e.Cancel = true;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }


